package com.example.RuanJianKeShe.Service.student;

import com.alibaba.fastjson.JSON;
import com.example.RuanJianKeShe.Bean.Course;
import com.example.RuanJianKeShe.Bean.Teacher;
import com.example.RuanJianKeShe.Dao.impl.StuCourseDaoImpl;
import com.example.RuanJianKeShe.Dao.impl.TeaCourseDaoImpl;
import com.example.RuanJianKeShe.Dao.impl.TeacherDaoImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;

@WebServlet("/getCourseBySno")
public class getCourseBySno extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("application/json;charset=utf-8");
        HttpSession session = req.getSession();
        Object mySno = session.getAttribute("mySno");
        StuCourseDaoImpl stuCourseDao = new StuCourseDaoImpl();
        List<Course> courseBySno = stuCourseDao.getCourseBySno(Integer.parseInt(mySno.toString()));
        TeaCourseDaoImpl teaCourseDao = new TeaCourseDaoImpl();
        TeacherDaoImpl teacherDao = new TeacherDaoImpl();
        List<Teacher> allTeacher = teacherDao.getAllTeacher();
        HashMap<String,Object> map;
        HashMap<String,Object>[] arr = new HashMap[courseBySno.size()];
        for (int i=0;i<courseBySno.size();i++){
            map = new HashMap<>();
            map.put("tno",teaCourseDao.getTeaCourse(courseBySno.get(i).getCno()).getTno());
            for (int j=0;j<allTeacher.size();j++){
                if (allTeacher.get(j).getTno()==teaCourseDao.getTeaCourse(courseBySno.get(i).getCno()).getTno()){
                    map.put("tname",allTeacher.get(j).getTname());
                    map.put("telephone",allTeacher.get(j).getTelephone());
                    map.put("email",allTeacher.get(j).getEmail());
                }
            }
            map.put("tno",teaCourseDao.getTeaCourse(courseBySno.get(i).getCno()).getTno());
            map.put("teachWeek",teaCourseDao.getTeaCourse(courseBySno.get(i).getCno()).getTeachWeek());
            map.put("teachTime",teaCourseDao.getTeaCourse(courseBySno.get(i).getCno()).getTeachTime());
            map.put("cno",courseBySno.get(i).getCno());
            map.put("cname",courseBySno.get(i).getCname());
            map.put("address",courseBySno.get(i).getAddress());
            map.put("credit",courseBySno.get(i).getCredit());
            arr[i] = map;
        }
        HashMap<String,Object> resultMap = new HashMap<>();
        resultMap.put("myCourseList",arr);
        String jsonString = JSON.toJSONString(resultMap);
        resp.getWriter().write(jsonString);


    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req,resp);
    }
}
